package com.example.controller;

import com.example.entity.Dormitory;
import com.example.entity.Student;
import com.example.mapper.DormitoryMapper;
import com.example.service.DormitoryService;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Dormitory")
public class DormitoryConstoller {
    @Autowired
    private DormitoryService dormitoryService;
    @Autowired
    private DormitoryMapper dormitoryMapper;

    @GetMapping
    public List<Dormitory> index() {
        List<Dormitory> all = dormitoryMapper.finAll();
        return all;
    }

    @PostMapping
    public Map<String, Object> save(@RequestBody Dormitory dormitory) {
        return  dormitoryService.saveDormitory(dormitory);
    }
    @DeleteMapping("/delete")
    public Map<String, Object> delete(@RequestParam("floor_num") String floor_num,
                          @RequestParam("layer") Integer layer,
                          @RequestParam("room_num") Integer room_num){
        return dormitoryService.deleteDormitory(floor_num,layer,room_num);
    }
    @GetMapping("/page")
    public Map<String, Object> findPage(@RequestParam Integer pageNum,
                                        @RequestParam Integer pageSize,
                                        @RequestParam String floor_num,
                                        @RequestParam String layer,
                                        @RequestParam String room_num) {
        Integer room_numValue = null;
        Integer layer_numValue = null;
        if (!room_num.isEmpty()) {
            room_numValue = Integer.parseInt(room_num);
        }
        if (!layer.isEmpty()) {
            layer_numValue = Integer.parseInt(layer);
        }
        pageNum = (pageNum - 1) * pageSize;
        List<Dormitory> data = dormitoryMapper.selectPage(pageNum, pageSize, floor_num,layer_numValue,room_numValue);
        Integer total = dormitoryMapper.selectTotal(floor_num,layer_numValue,room_numValue);
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }
}
